<?php


class MobileSms {

    public function send($mobile, $content) {
        //短信接口配置
        $sn="SDK-WSS-010-07092";
        $pwd="5-a=64-4";
        //$content2 = iconv("UTF-8", "gb2312", $content . ' 如有疑问请拨打客服电话：4006100088 [WishBird千纸鹤]');
        //$content2 = iconv("UTF-8", "gb2312", $content);
        //$content2 = urlencode($content2);
        $content=$content."【去保养】";
        $pwd = strtoupper(md5($sn .$pwd));
        $url = 'http://sdk2.entinfo.cn:8061/mdsmssend.ashx?sn='.$sn.'&pwd=' .$pwd. '&mobile=' . $mobile . '&content=' . $content . '&ext=&rrid=&stime=';

        $dataSms['out_id']=9999999999;
        $dataSms['to_phone']=$mobile;
        $dataSms['content']=$content;
        $dataSms['create_time']=time();
        M("sms")->add($dataSms);

        /*return 9999999999;
        exit; */
        $rs = $this->fopen_url($url);
        $t = substr($rs, 0, 1);
        if ($t == '-' || $rs == '') {
            return false;
        } else {
            return $rs;
        }
    }

    public function get() {
        //该demo的功能是取客户的上行，即回复信息
        $flag = 0;
        $sn="SDK-WSS-010-07092";
        $pwd="5-a=64-4";
        //要post的数据 
        $argv = array(
            'sn' => $sn, ////替换成您自己的序列号
            'pwd' => strtoupper(md5($sn . $pwd)) //此处密码需要加密 加密方式为 md5(sn+password) 32位大写
        );
        //构造要post的字符串 
        $params = '';
        foreach ($argv as $key => $value) {
            if ($flag != 0) {
                $params .= "&";
                $flag = 1;
            }
            $params.= $key . "=";
            $params.=urlencode($value);
            $flag = 1;
        }
        $length = strlen($params);
        //创建socket连接 
        $fp = fsockopen("sdk2.zucp.net", 8060, $errno, $errstr, 10) or exit($errstr . "--->" . $errno);
        //构造post请求的头 
        $header = "POST /webservice.asmx/mo HTTP/1.1\r\n";
        $header .= "Host:sdk2.zucp.net\r\n";
        $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
        $header .= "Content-Length: " . $length . "\r\n";
        $header .= "Connection: Close\r\n\r\n";
        //添加post的字符串 
        $header .= $params . "\r\n";
        //发送post的数据 
        $line = '';
        fputs($fp, $header);
        while (!feof($fp)) {
            $line .= fgets($fp, 1024);
        } list(, $line) = explode("\r\n\r\n", $line, 2);
        //<string xmlns="http://tempuri.org/">-5</string>
        $line = str_replace("<string xmlns=\"http://tempuri.org/\">", "", $line);
        $line = str_replace("</string>", "", $line);
        $line = preg_replace('/<[^>]*?>/', '', $line);
        $line = trim($line);
        // echo $line; exit;//该行调试代码用，直接显示mo的返回值，不做任何处理
        // for( $i = 0; $i < count( explode("\n",$line) ) ;$i++)

        if ($line == "1") {
            echo "返回值是1，无上行内容";
            exit;
        }

        //先按照 \n 把回复的内容拆成多条
        $reply_arr = explode("\n", $line);

        $num = count($reply_arr);

        $y = 1;
        $Sms = M("Sms");
        for ($i = 0; $i < $num; $i++) {
            //再按照半角逗号，把一条短信的每一项拆出来
            $reply = explode(",", $reply_arr[$i]);
            echo "第" . $y . "条<br />";
            echo "<b>回复：</b>" . $reply[1] . "<br />";
            echo "<b>回复人：</b>" . $reply[2] . "<br />";
            echo "<b>回复内容：</b>" . iconv("gb2312", "UTF-8//IGNORE", urldecode($reply[3])) . "<br />";
            $time = substr($reply[4], 0, 19);
            echo "<b>回复时间：</b>" . $time . "<br /><br /><br /><br />";

            $data["out_id"] = $reply[1];
            $data["type"] = "2";
            $data["from_phone"] = $reply[2];
            $data["content"] = iconv("gb2312", "UTF-8//IGNORE", urldecode($reply[3]));
            $data["user_time"] = strtotime($time);
            $data["create_time"] = time();
            $Sms->add($data);
            $y++;
        }
    }

    function fopen_url($url) {
        if (function_exists('file_get_contents')) {
            $file_content = @file_get_contents($url);
        } elseif (ini_get('allow_url_fopen') && ($file = @fopen($url, 'rb'))) {
            $i = 0;
            while (!feof($file) && $i++ < 1000) {
                $file_content .= strtolower(fread($file, 4096));
            }
            fclose($file);
        } elseif (function_exists('curl_init')) {
            $curl_handle = curl_init();
            curl_setopt($curl_handle, CURLOPT_URL, $url);
            curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
            curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl_handle, CURLOPT_FAILONERROR, 1);
            curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Trackback Spam Check'); //引用垃圾邮件检查
            $file_content = curl_exec($curl_handle);
            curl_close($curl_handle);
        } else {
            $file_content = '';
        }
        return $file_content;
    }

}